<?php
//  企业微信

namespace app\service;

class WechatQyService
{
    public static ?WechatQyService $_instance = null;

    protected string $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=owenweb';

    public function __construct()
    {
    }

    /**
     * @return WechatQyService|mixed
     */
    public static function instance(): ?WechatQyService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    /** 企微群消息推送
     * @param string $msg
     * @param array  $at_who
     * @return bool
     */
    public function textSend(string $msg = '', array $at_who = []): bool
    {
        $_params = [
            'msgtype' => 'text',
            'text'    => [
                'content'        => $msg,
                "mentioned_list" => $at_who,
            ],
        ];
        $params  = json_encode($_params);
        $ch      = curl_init($this->url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_exec($ch);
        curl_close($ch);
        return true;
    }
}